package com.crm.service.impl.customerManager;

import com.crm.dao.mapper.customMapper.customerManagerCustomMapper.CustomerReceiveCustomMapper;
import com.crm.dao.mapper.customerManager.CustomerReceiveMapper;
import com.crm.model.entity.customerManager.CustomerReceive;
import com.crm.model.entity.customerManager.CustomerReceiveExample;
import com.crm.model.qo.customerManager.AnalyseCustomerQO;
import com.crm.service.customerManager.CustomerReceiveDbService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ Author ：pym
 * @ Date   ：2021/1/28 16:32
 */
@Service
public class CustomerReceiveDbServiceImpl implements CustomerReceiveDbService {

    @Autowired
    CustomerReceiveMapper customerReceiveMapper;

    @Autowired
    CustomerReceiveCustomMapper customerReceiveCustomMapper;

    @Override
    public long selectCustomerReceiveCount(String accountId, String customerPoolId) {
        CustomerReceiveExample customerReceiveExample = new CustomerReceiveExample();
        CustomerReceiveExample.Criteria criteria = customerReceiveExample.createCriteria();
        criteria.andReceivePeopleIdEqualTo(accountId);
        criteria.andReceiveWayEqualTo(1);
        criteria.andCustomerPoolFromIdEqualTo(customerPoolId);
        return customerReceiveMapper.countByExample(customerReceiveExample);
    }

    /**
     * 通过用户id和客户id查询领取记录
     * @param accountId 用户id
     * @param customerPoolId 客户池id
     * @return
     */
    @Override
    public CustomerReceive selectCustomerReceiveNew(String accountId, String customerPoolId) {
        CustomerReceiveExample customerReceiveExample = new CustomerReceiveExample();
        CustomerReceiveExample.Criteria criteria = customerReceiveExample.createCriteria();
        criteria.andReceivePeopleIdEqualTo(accountId);
        criteria.andReceiveWayEqualTo(1);
        criteria.andCustomerPoolFromIdEqualTo(customerPoolId);
        customerReceiveExample.setOrderByClause(" receive_tm desc ");
        List<CustomerReceive> customerReceives = customerReceiveMapper.selectByExample(customerReceiveExample);
        if(customerReceives != null && !customerReceives.isEmpty()){
            return customerReceives.get(0);
        }
        return null;
    }

    @Override
    public Boolean insertCustomerReceive(CustomerReceive customerReceive) {
        return customerReceiveMapper.insert(customerReceive) > 0;
    }

    /**
     * 根据领取人id查询领取的客户id
     * @param qo
     * @return
     */
    @Override
    public List<Long> getReceiveListByUserId(AnalyseCustomerQO qo) {
        return customerReceiveCustomMapper.getReceiveListByUserId(qo);
    }
}
